home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JMenuItem;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.MenuElement;
- import com.sun.java.swing.MenuSelectionManager;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.MenuItemUI;
- import com.sun.java.swing.plaf.UIResource;
- import java.awt.AWTEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Point;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.io.Serializable;
-
- public class BasicMenuItemUI extends MenuItemUI implements Serializable {
- protected static Color pressedBackground;
- protected static Color pressedForeground;
- protected static final int defaultTextIconGap = 4;
- protected MouseListener mouseListener;
- protected MouseMotionListener dragListener;
- protected Icon menuArrow = null;
- protected Icon checkIcon = null;
- protected boolean oldBorderPainted;
-
- protected void addListeners(JComponent c) {
- ((Component)c).addMouseListener(this.mouseListener);
- ((Component)c).addMouseMotionListener(this.dragListener);
- }
-
- protected MouseListener createMouseListener(JComponent c) {
- return new BasicMenuMouseListener();
- }
-
- protected MouseMotionListener createMouseMotionListener(JComponent c) {
- return new BasicMenuMouseMotionListener();
- }
-
- public static ComponentUI createUI(JComponent c) {
- return new BasicMenuItemUI();
- }
-
- Icon getCheckIcon() {
- return this.checkIcon;
- }
-
- public Insets getDefaultMargin(AbstractButton c) {
- return new Insets(2, 2, 2, 2);
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- Icon getMenuArrow() {
- return this.menuArrow;
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- Icon checkIcon = this.getCheckIcon();
- Icon arrowIcon = this.getMenuArrow();
- return BasicGraphicsUtils.getPreferredMenuItemSize(c, checkIcon, arrowIcon, 4);
- }
-
- protected void initListeners(JComponent c) {
- this.mouseListener = this.createMouseListener(c);
- this.dragListener = this.createMouseMotionListener(c);
- }
-
- public void installUI(JComponent c) {
- JMenuItem menuItem = (JMenuItem)c;
- this.initListeners(c);
- this.addListeners(c);
- c.setOpaque(true);
- LookAndFeel.installBorder(c, "MenuItem.border");
- this.oldBorderPainted = ((AbstractButton)menuItem).isBorderPainted();
- ((AbstractButton)menuItem).setBorderPainted((Boolean)UIManager.get("MenuItem.borderPainted"));
- LookAndFeel.installColorsAndFont(c, "MenuItem.background", "MenuItem.foreground", "MenuItem.font");
- if (pressedBackground == null || pressedBackground instanceof UIResource) {
- pressedBackground = UIManager.getColor("MenuItem.pressedBackground");
- }
-
- if (pressedForeground == null || pressedForeground instanceof UIResource) {
- pressedForeground = UIManager.getColor("MenuItem.pressedForeground");
- }
-
- if (this.menuArrow == null || this.menuArrow instanceof UIResource) {
- this.menuArrow = UIManager.getIcon("MenuItem.arrowIcon");
- }
-
- if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
- this.checkIcon = UIManager.getIcon("MenuItem.checkIcon");
- }
-
- }
-
- private int lower(int ascii) {
- return ascii >= 65 && ascii <= 90 ? ascii + 97 - 65 : ascii;
- }
-
- public void paint(Graphics g, JComponent c) {
- BasicGraphicsUtils.paintMenuItem(g, c, this.getCheckIcon(), this.getMenuArrow(), pressedBackground, pressedForeground, 4);
- }
-
- public void processKeyEvent(JMenuItem item, KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
- int key = ((AbstractButton)item).getMnemonic();
- if (key != 0) {
- if (((AWTEvent)e).getID() == 401 && this.lower(key) == this.lower(e.getKeyChar())) {
- manager.clearSelectedPath();
- ((AbstractButton)item).doClick(0);
- ((InputEvent)e).consume();
- }
-
- }
- }
-
- public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement[] path, MenuSelectionManager manager) {
- Point p = e.getPoint();
- if (p.x >= 0 && p.x < ((JComponent)item).getWidth() && p.y >= 0 && p.y < ((JComponent)item).getHeight()) {
- if (((AWTEvent)e).getID() == 502) {
- manager.clearSelectedPath();
- ((AbstractButton)item).doClick(0);
- } else {
- manager.setSelectedPath(path);
- }
- } else if (((AWTEvent)e).getID() == 502) {
- manager.clearSelectedPath();
- } else if (((AbstractButton)item).getModel().isArmed()) {
- MenuElement[] newPath = new MenuElement[path.length - 1];
- int i = 0;
-
- for(int c = path.length - 1; i < c; ++i) {
- newPath[i] = path[i];
- }
-
- manager.setSelectedPath(newPath);
- }
-
- }
-
- protected void removeListeners(JComponent c) {
- ((Component)c).removeMouseListener(this.mouseListener);
- ((Component)c).removeMouseMotionListener(this.dragListener);
- }
-
- public void uninstallUI(JComponent c) {
- this.removeListeners(c);
- LookAndFeel.uninstallBorder(c);
- ((JMenuItem)c).setBorderPainted(this.oldBorderPainted);
- if (this.menuArrow instanceof UIResource) {
- this.menuArrow = null;
- }
-
- if (this.checkIcon instanceof UIResource) {
- this.checkIcon = null;
- }
-
- }
-
- public void update(Graphics g, JComponent c) {
- this.paint(g, c);
- }
- }
-